package com.shu.qq.data.entity;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.*;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User implements Serializable, UserDetails {
    private static final long serialVersionUID = 1L;
    @TableId
    private String account;
    private String nickname;
    private String password;
    private String roles; //保存形式为json的数组。"[\"ROLE_USER\",\"ROLE_ADMIN\"]"
    @TableField(value = "create_time", fill = FieldFill.INSERT)
    private Date createTime;
    @TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;

    /**
     * 隐私过滤，重要信息不返回给用户
     */
    public Map<String,String> privacyFilter(){
        HashMap<String, String> user = new HashMap<>();
        user.put("account",this.getAccount());
        user.put("nickname",this.getNickname());
        user.put("roles",this.getRoles());
        user.put("createTime",this.getCreateTime().toString());
        user.put("updateTime",this.getUpdateTime().toString());
        return user;
    }

    /**
     * 所拥有的权限
     */
    @TableField(exist = false)
    private List<GrantedAuthority> authorities;

    public void setAuthorities(List<GrantedAuthority> authorities) {
        this.authorities = authorities;
    }
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.authorities;
    }

    @Override
    public String getUsername() {
        return this.account;
    }

    /**
     * 账户是否未过期，过期无法验证
     * */
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * 指定用户是否解锁，锁定的用户无法进行身份认证
     * */
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    /**
     * 指示是否已过期的用户的凭据（密码），过期的凭据防伪认证
     * */
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * 是否可用
     * */
    @Override
    public boolean isEnabled() {
        return true;
    }
}
